
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>
#include "dht.h"


//initialize the variables

struct sockaddr_in this_client_info;
struct sockaddr_in dht_node_info;
struct hostent *dht_node_host;
int dht_node_port;
int this_client_socket;
int bytes_recieved;
char dir_id[64];
char dht_node_ip[64];
char send_data[1024],recv_data[1024];

int main(int argc, char *argv[])
{
	if (argc !=3) {
		
	  printf("Usage: \n %s <dhtnode> <id>\n", argv[0]);
	  
	} else {
	  
	  // Correctly format dhtnode.
	  
	  char dht_node_input[64];
	  char *dht_node_in;
	  //get input from the user
	  strncpy(dht_node_input, argv[1], 64-1);
	  
	  dht_node_in = strtok(dht_node_input, ":");
	  
	  if(dht_node_in)
	    {
	      strncpy(dht_node_ip, dht_node_in, 64-1);
	    	printf("DHT Node IP\t: %s\n", dht_node_ip);
	    }
	  dht_node_in = strtok(NULL, ":");
	  
	  if(dht_node_in) {
	    dht_node_port = atoi(dht_node_in);
	    printf("DHT Node Port\t: %i\n", dht_node_port);
	  }
	  
	  // Correctly format id.
	  
	    strncpy(dir_id, argv[2], 64-1);
	    //get the ID
	    printf("The ID is\t: %s\n", dir_id);
	    
	    //saving things to s to send to the node
	    size_t len1 = strlen(dir_id);
	    char *s = malloc(len1+ 2);
	    memcpy(s, "3|", 2);
	    memcpy(s + 2, dir_id, len1);
        
	    int p = node_connect_rm(dht_node_ip, dht_node_port, s);
	      
	}
}


int node_connect_rm(int dst_ip[64], int dst_port, char send_data[512]){
    //creating socket using TCP
    if ((this_client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
        perror("Socket Error");
        exit(EXIT_FAILURE);
    }
    
    //dht_node_host = gethostbyname(dst_ip);
    
    this_client_info.sin_family = AF_INET; 
    inet_aton(dst_ip, &(this_client_info.sin_addr)); 
    this_client_info.sin_port = htons(dst_port);
    bzero(&(this_client_info.sin_zero), 8);
    printf("connected...\n");
    
    if (connect(this_client_socket, (struct sockaddr *)&this_client_info,
                sizeof(struct sockaddr)) == -1)
    {
        perror("Connect Error\t");
        exit(1);
    }
    
    send(this_client_socket,send_data,strlen(send_data), 0);   
    //close(this_client_socket);
    
    while(1)
    {
        
        bytes_recieved=recv(this_client_socket,recv_data,1024,0);
        recv_data[bytes_recieved] = '\0';
        
        if (bytes_recieved > 0) {
            printf("\nRecieved data = %s\n" , recv_data);
            close(this_client_socket);
            
            //printf("\nSEND (q or Q to quit) : ");
            fflush(stdout);
            break;
        }
        //gets(send_data);
        /*    
         if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
         send(this_client_socket,send_data,strlen(send_data), 0); 
         
         else
         {
         send(this_client_socket,send_data,strlen(send_data), 0);   
         close(this_client_socket);
         break;
         }
         */
    }  
    return 0;
    
	
}

